package com.qd.panda.evaluate;

import com.cdqidi.dto.ApiResult;
import com.cdqidi.dto.ApiResultWrapper;
import com.cdqidi.dto.DelDTO;
import com.cdqidi.exception.ApiException;
import com.cdqidi.valid.group.DefaultGroup;
import com.qd.common.panda.domain.entity.score.CarParkEvaluateDTO;
import com.qd.common.panda.domain.entity.score.CarParkScoreEvaluationLabelDTO;
import com.qd.common.sys.util.SecurityUtils;
import com.qd.panda.model.CarParkEvaluate;
import com.qd.panda.service.score.CarParkEvaluateService;
import com.qd.panda.service.score.CarParkScoreEvaluationLabelService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;
import java.util.Optional;

/**
 * APP_停车场评价
 *
 * @author sjk
 */
@RequiredArgsConstructor
@Validated
@RestController
@RequestMapping("/app/carParkEvaluate")
@Slf4j
public class AppCarParkEvaluateController {
    private final CarParkEvaluateService carParkEvaluateService;

    private final CarParkScoreEvaluationLabelService carParkScoreEvaluationLabelService;

    /**
     * 添加
     *
     * @param dto 页面对象
     * @return 页面对象
     */
    @PostMapping(value = "/save")
    public ApiResultWrapper<Object> save(@Validated({DefaultGroup.class}) @RequestBody CarParkEvaluateDTO dto) {
        carParkEvaluateService.saveList(dto);
        dto.freeData();
        return ApiResult.success("评价成功");
    }

    /**
     * 删除
     *
     * @param delDTO 主键，多个用分号隔开
     * @return 页面对象
     */
    @DeleteMapping(value = "/delete")
    public ApiResultWrapper<Object> delete(@Validated @RequestBody DelDTO<String> delDTO) {
        final List<String> ids = delDTO.getIds();
        if (ids.size() == 1) {
            CarParkEvaluate carParkEvaluate = Optional.ofNullable(carParkEvaluateService.getById(ids.get(0))).orElseThrow(() -> new ApiException("评价不存在"));
            if (!carParkEvaluate.getUserId().equals(SecurityUtils.getLoginUser().getUserId())) {
                throw new ApiException("没有权限删除");
            }
            carParkEvaluateService.removeById(carParkEvaluate.getEid());
        } else {
            return ApiResult.error("不支持批量删除");
        }
        return ApiResult.success("删除成功");
    }

    /**
     * 获得评价标签
     *
     * @return 页面对象
     */
    @GetMapping(value = "/getLabel")
    public Map<Integer, List<CarParkScoreEvaluationLabelDTO>> getLabel() {
        return carParkScoreEvaluationLabelService.getList();
    }
}